﻿using System.IO.Compression;
using System.Net;
using System.Security.Cryptography.X509Certificates;

namespace _093.SmbFile;

class Program {
    static void Main() {
        // 输入要打包的文件夹路径和目标目录路径
        string sourceFolderPath = @"D:1.txt";
        string destinationFolderPath = @"127.0.0.1";

        if (!Directory.Exists(destinationFolderPath)) {
            Directory.CreateDirectory(destinationFolderPath);
        }
        string shareName = "Folder";
        string uncPath = GetUncPath(destinationFolderPath, shareName);

        //string[] files = new []{};

        // 构造目标Zip文件的路径
        string zipFilePath = Path.Combine(uncPath, "PackedFiles.zip");

        // 创建一个新的Zip文件并添加文件
        using (ZipArchive archive = ZipFile.Open(zipFilePath, ZipArchiveMode.Create)) {
            archive.CreateEntryFromFile(sourceFolderPath, Path.GetFileName(sourceFolderPath));
        }

        Console.WriteLine("文件已打包成Zip并保存到目标目录。");
    }


    // 访问SMB共享文件夹
    public static void Smb(string localFilePath, string ipAddress) {
        // 本地文件路径
        //string localFilePath = @"C:\Path\To\Your\File.txt";
        // Windows共享文件夹的IP地址
        //string ipAddress = "192.168.1.2"; // 请替换为实际的IP地址

        // 共享文件夹的共享名称
        string shareName = "Share"; // 请替换为实际的共享名称

        // 共享文件夹的用户名和密码
        string username = "your_username";
        string password = "your_password";

        // 构建UNC路径
        string uncPath = GetUncPath(ipAddress, shareName);

        // 使用NetworkCredential提供用户名和密码
        //NetworkCredential networkCredential = new NetworkCredential(username, password);
        //// 设置访问共享文件夹的网络凭据
        //CredentialCache credentialCache = new CredentialCache
        //{
        //    { new Uri(uncPath), "Basic", networkCredential }
        //};
        //CredentialCache credentialCache = new CredentialCache();
        //credentialCache.Add(new Uri(uncPath), "Basic", new NetworkCredential(username, password));

        // 将文件复制到共享文件夹
        string fileName = Path.GetFileName(localFilePath);
        string destinationPath = Path.Combine(uncPath, fileName);

        File.Copy(localFilePath, destinationPath, true); // 第三个参数为true表示允许覆盖现有文件

        Console.WriteLine($"File '{fileName}' copied to '{uncPath}' successfully.");
    }

    // 辅助方法：获取UNC路径
    private static string GetUncPath(string ipAddress, string shareName) {
        // 构建UNC路径
        return $@"\\{ipAddress}\{shareName}";
    }
}